package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.constants.SystemConstants;
import com.example.domain.entity.Follow;
import com.example.domain.entity.Plant;
import com.example.domain.entity.User;
import com.example.exception.FloraException;
import com.example.mapper.FollowMapper;
import com.example.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.service.IPlantService;
import com.example.service.UserService;
import com.example.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zrr
 * @since 2022-12-02
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {
    @Autowired
    IPlantService plantService;
    @Autowired
    UserServiceImpl userService;


    @Autowired
    RedisCache redisCache;

    @Override
    public IPage getFollowingList(long followerId, Integer followingType, Integer pageNum, Integer pageSize) {

        LambdaQueryWrapper<Follow> lqw1=new LambdaQueryWrapper<>();
        lqw1.eq(Follow::getFollowerId,followerId);
        lqw1.eq(Follow::getFollowingType,followingType);
        List<Follow> followList= this.baseMapper.selectList(lqw1);
        List<Long> followingIdList=followList.stream().map(Follow::getFollowingId).collect(Collectors.toList());

        if(followingType.equals(SystemConstants.FOLLOW_TYPE_PLANT) ){
            Page<Plant> page=new Page<>(pageNum,pageSize);
            LambdaQueryWrapper<Plant> lqw2=new LambdaQueryWrapper<>();
            lqw2.in(Plant::getPlantId,followingIdList);
            plantService.page(page,lqw2);
            return page;
        }else {
            Page<User> page=new Page<User>(pageNum,pageSize);
            LambdaQueryWrapper<User> lqw2=new LambdaQueryWrapper<User>();
            lqw2.in(User::getId,followingIdList);
            userService.page(page,lqw2);
            return page;
        }

    }

    @Override
    public Long getFollowerNumOfPlant(long plantId) {
        return Long.valueOf(redisCache.getCacheMapValue("PlantFollowerNum",String.valueOf(plantId)))  ;
    }

    @Override
    public Long getFollowerNumOfUser(long userId) {
        return null;
    }

    @Override
    @Transactional
    public void addFollowerOfPlant(Follow follow) throws FloraException {

        try{
            this.baseMapper.insert(follow);
            redisCache.incrementCacheMapValue("PlantFollowerNum",follow.getFollowingId().toString(),1);
        }catch (Exception e){
            throw new FloraException("关注植物失败"+e.getMessage());

        }

    }

    @Override
    public void addFollowerOfUser(Follow follow) throws FloraException {

    }

    @Override

    public void deleteFollowerOfPlant(Follow follow) {
        LambdaQueryWrapper<Follow> lqw=new LambdaQueryWrapper();
        lqw.eq(Follow::getFollowerId,follow.getFollowerId())
                .eq(Follow::getFollowingId,follow.getFollowingId())
                .eq(Follow::getFollowingType,follow.getFollowingType());
        this.baseMapper.delete(lqw);
        redisCache.incrementCacheMapValue("PlantFollowerNum",follow.getFollowingId().toString(),-1);
    }

    @Override
    public void deleteFollowerOfUser(Follow follow) {

    }
}
